package com.weixing.mall.provider.util;

import com.google.common.collect.Lists;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.base.exception.BusinessException;
import com.weixing.mall.provider.api.model.vo.AgentRpcVo;
import com.weixing.mall.provider.model.enums.AuditDeptEnum;
import com.weixing.mall.provider.model.enums.OrderIsClaimEnum;
import com.weixing.mall.provider.model.enums.OrderStateEnum;
import com.weixing.mall.provider.model.query.AfterSaleQuery;
import com.weixing.mall.provider.model.query.AgentOrderQuery;
import com.weixing.mall.provider.model.query.AgentStatisticQuery;
import com.weixing.mall.provider.model.query.agent.AgentOrderStatisticQuery;

import java.util.List;

/**
 * 代理订单工具类
 *
 * @author siashan
 * @since v2.0.1
 **/
public class AgentOrderUtil {

    /**
     * 封装统计查询参数
     *
     * @param agentId  代理商id
     * @param sq        前台传递的查询参数
     * @return  包装后的查询参数
     */
    public static AgentStatisticQuery wrapStatisticQuery(Long agentId, AgentOrderStatisticQuery sq) {
        AgentStatisticQuery query = new AgentStatisticQuery();
        query.setIsPay(WhetherEnum.YES);
        query.setIsDel(WhetherEnum.NO);
        query.setOrderType(sq.getOrderType());
        // 1全部  2 已提交  3 待审核  4 已审核  5 已认领
        switch (sq.getType()) {
            case 1:
                // 全部订单
                query.setAgentIdForQueryAll(agentId);
                break;
            case 2:
                // 个人订单
                query.setAgentIdForQueryAll(null);
                query.setAgentId(agentId);
                break;
            case 3:
                // 待审核订单
                query.setAuditDept(AuditDeptEnum.AGENT);
                query.setAuditor(agentId);
                List<Integer> states = Lists.newArrayList();
                states.add(OrderStateEnum.HAS_PAY.getValue());
                states.add(OrderStateEnum.AUDITING.getValue());
                query.setOrderStates(states);
                break;
            case 4:
                // 已审核订单
                query.setHasAuditAgentId(agentId);
                break;
            case 5:
                // 已认领订单
                query.setAgentId(agentId);
                query.setIsClaim(OrderIsClaimEnum.HAS_CLAIM);
            default:
                break;
        }
        return query;
    }

    public static void wripeQuery(AgentOrderQuery query, AgentRpcVo agentVo) {
        Integer level = agentVo.getAgentLevel();
        query.setDealerId(null);
        query.setLevel1st(null);
        query.setLevel2nd(null);
        query.setLevel3rd(null);
        switch (level) {
            case 1:
                query.setDealerId(agentVo.getId());
                break;
            case 2:
                query.setLevel1st(agentVo.getId());
                break;
            case 3:
                query.setLevel2nd(agentVo.getId());
                break;
            case 4:
                query.setLevel3rd(agentVo.getId());
            default:
                break;
        }
    }

    public static void wripeQuery(AfterSaleQuery query, AgentRpcVo agentVo) {
        Integer level = agentVo.getAgentLevel();
        query.setDealerId(null);
        query.setLevel1st(null);
        query.setLevel2nd(null);
        query.setLevel3rd(null);
        switch (level) {
            case 1:
                query.setDealerId(agentVo.getId());
                break;
            case 2:
                query.setLevel1st(agentVo.getId());
                break;
            case 3:
                query.setLevel2nd(agentVo.getId());
                break;
            case 4:
                query.setLevel3rd(agentVo.getId());
            default:
                break;
        }
    }


    public static void wrapSaleQuery(String type,AgentOrderQuery query, AgentRpcVo agentRpcVo) {
        if ("own".equals(type)) {
            query.setAgentId(agentRpcVo.getId());
        } else if ("group".equals(type)) {
            wripeQuery(query,agentRpcVo);
        } else {
            throw  new BusinessException("非法请求");
        }
    }
}
